import Mock from 'mockjs'
import books from './books.json'
import goods from './goods.json'
import goodslist from './goodslist.json'
import users from './users.json'

Mock.mock('/app/login','post',(options)=>{
    const {name,pwd} = JSON.parse(options.body);
    console.log(options.body)

    const user = users.find(u=>u.username ===name&&u.password==pwd);
   
    if(user){
        //console.log(user);
        return{
            status:200,
            data:user,
            flag:true,
            msg:'ok'
        }
    }
    else{
        return{
            msg:'error',
           flag:false
        }
    }

})


 Mock.mock('/mock/getbooks','get',{
    code:200,
    data:books
})
let sbooks =[]
Mock.mock('/mock/searchbooks','post',(options)=>{
    const {name} = JSON.parse(options.body);
    sbooks = books.filter(b=>b.name.indexOf(name) !==-1)
    console.log(sbooks)
    return {
        status:200,
        msg:'找到了',
        data:sbooks
    }
})
Mock.mock('/app/addbook','post',(options)=>{
    const {name,author} =JSON.parse(options.body);
    if(author!==''&&author!==''){
        books.push({id:books.length+1,name:name,author:author});
        console.log(books)
        return {
            data:books,
            msg:'添加成功',
            status:200
        }
    }
    else{
       alert('内容不能为空')
        return{
            data:books,
            msg:'失败',
            status:400
        }
    }
})



Mock.mock('/mock/getgoods','get',{
    code:200,
    message:'成功获取数据',
    'data|3-5':goods
})



// 模拟商品列表
const productList = [];

// 模拟添加商品的接口
Mock.mock('/api/addProduct', 'post', function (options) {
    const newProduct = JSON.parse(options.body);
    productList.push(newProduct);
    return {
        status: 200,
        message: '商品添加成功',
        data: newProduct
    };
});

// 模拟获取商品列表的接口（可选，用于验证添加结果）
Mock.mock('/api/getProducts', 'get', {
    status: 200,
    message: '获取商品列表成功',
    data: productList
});